// 完善正则表达式
const regs = {
    phone: /^1[3-9]\d{9}$/, // 手机号正则，以1开头，第二位是3-9之间的数字，后面跟9个数字
    email: /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/, // 邮箱正则
    number: /^\d+$/, // 数字正则
    password:   /^(?![a-zA-Z]+$)(?!\d+$)(?![^\da-zA-Z\s]+$).{1,9}$/, // 密码正则，至少8位，包含数字、字母和特殊字符
    account: /^[a-zA-Z0-9_]{1,}$/, // 账号正则，4-20位字母或数字
    ip: /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/, // IP正则
    pInt: /^[1-9]\d*$/, // 正整数正则
    pointNumber: /^\d+\.\d+$/, // 小数正则
    money: /^(?:\d{1,3}(?:,\d{3})*|\d+)(?:\.\d{1,2})?$/, // 金额正则，支持千分位分隔符
    vmName: /^[a-zA-Z][a-zA-Z0-9_]{0,29}$/, // VM名称正则，字母开头，字母数字下划线，不超过30位
}

const verify = (rule, value, reg, callback) => {
    if (value) {
        if (reg.test(value)) {
            callback()
        } else {
            callback(new Error(rule.message))
        }
    } else {
        callback()
    }
}

// 完善表单验证规则
export default {
    phone: (rule, value, callback) => {
        return verify(rule, value, regs.phone, callback)
    },
    email: (rule, value, callback) => {
        return verify(rule, value, regs.email, callback)
    },
    number: (rule, value, callback) => {
        return verify(rule, value, regs.number, callback)
    },
    password: (rule, value, callback) => {
        return verify(rule, value, regs.password, callback)
    },
    account: (rule, value, callback) => {
        return verify(rule, value, regs.account, callback)
    },
    ip: (rule, value, callback) => {
        return verify(rule, value, regs.ip, callback)
    },
    pInt: (rule, value, callback) => {
        return verify(rule, value, regs.pInt, callback)
    },
    pointNumber: (rule, value, callback) => {
        return verify(rule, value, regs.pointNumber, callback)
    },
    money: (rule, value, callback) => {
        return verify(rule, value, regs.money, callback)
    },
    vmName: (rule, value, callback) => {
        return verify(rule, value, regs.vmName, callback)
    },
}